package cn.yeziji.forum.factory.websocket.parts;

import cn.yeziji.forum.bo.chatroom.ChatPrivateMsgBO;
import cn.yeziji.forum.common.websocket.Chatroom;
import cn.yeziji.forum.factory.websocket.WebsocketOperationAbstract;
import cn.yeziji.forum.factory.websocket.WebsocketOperationInterface;
import cn.yeziji.forum.factory.websocket.enums.WebsocketFactoryOption;
import com.github.benmanes.caffeine.cache.Cache;
import io.jsonwebtoken.lang.Assert;

/**
 * @author gzkemays
 * @date 2022/4/26
 * @deprecated to netty
 */
public class SystemMsgPart extends WebsocketOperationAbstract<Chatroom>
    implements WebsocketOperationInterface {

  public SystemMsgPart(Object data) {
    super(data);
  }

  public SystemMsgPart(Object data, Object additional) {
    super(data, additional);
  }

  /**
   * 系统推送消息
   *
   * <ul>
   *   信息推送数据要求
   *   <li>additional 为推送用户的 id 集合
   *   <li>msg 为系统推送的内容
   * </ul>
   */
  @Override
  protected Object defaultMethod() {
    Assert.notNull(additional);
    String userId = String.valueOf(additional);
    Cache<String, String> userCache = getUserCache();
    String token = userCache.getIfPresent(userId);
    if (token != null) {
      // 如果用户在线，则直接发送并缓存。
      send(token, params, JSON);
    } else {
      // 如果不在线，则将信息存储入缓存。
      saveChatroomCache(params);
    }
    return null;
  }

  @Override
  public Object operation(WebsocketFactoryOption option) {
    return invoke(getClass(), option);
  }
}
